<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.baihui.taobao.db.GeneDAO" %>
<%@ page import="com.baihui.taobao.db.DBConnectionMgr" %>
<%@ page import="com.baihui.taobao.AppConstants" %>
<%@ page import="com.baihui.taobao.db.CommonDAO" %>
<%@ page import="com.baihui.taobao.AppConstants" %>
<%@ page import="com.baihui.taobao.authorize.TaoBaoSessionInfo" %>
<%@ page import="com.baihui.taobao.transition.UserCase" %>
<%@ page import="com.baihui.taobao.util.Util" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<html>
<head>
<title></title>
</head>
<body>

<%
		TaoBaoSessionInfo sessionInfo = (TaoBaoSessionInfo) request.getSession().getAttribute(AppConstants.TOPSESSIONINFO);
		UserCase aCase = null;
		if (sessionInfo != null) {
		    aCase = new UserCase(sessionInfo);
		}
	    String secret = "124845593c5262a6cf91887a1d6a3fb42c41ece9";
	    String sign = request.getParameter("SIGN");
	    //System.out.println("SIGN:" + sign);
		//String userID=(String)session.getAttribute("USERID");
		String userID = request.getParameter("USERID");
        CommonDAO commonDAO = new CommonDAO();
        List jobList = commonDAO.findAllObject("select tjob.*,ttrig.*,topsession.* from tjob left outer join (select * from ttriggle where userid='" + userID + "') ttrig on tjob.jobid=ttrig.jobid left outer join topsession on topsession.baihuiuser=ttrig.userid");
%>

<table width="100%" border="1" align="center">
  <tr>
    <td colspan="4" align="center"><h2>数据同步计划</h2></td>
  </tr>
  <tr>
    <td width="6%" align="center"><strong>对象</strong></td>
    <td width="32%" align="center"><strong>同步周期</strong></td>
    <td width="38%" align="center"><strong>更新时间</strong></td>
    <td width="24%" align="center"><strong>操作</strong></td>
  </tr>

  <% 
  	for(int nIndex=0;nIndex<jobList.size();nIndex++)  {
  		Map row=(Map)jobList.get(nIndex);
  		int cronExp=180;
  		if(row.get("cronExp")!=null) {
  			cronExp=((Integer)row.get("cronExp")).intValue();
  		}
  %>
  <form method="post" >
  	<input type="hidden" value="<%=row.get("jobid")%>"/>
	  <tr>
	    <td align="center" ><%=row.get("jobname")%></td>
	    <td align="center" >周期:
	      <input id="schuduer_time_<%=row.get("jobid")%>" name="schuduer_time_<%=row.get("jobid")%>" type="text" disabled="true" value="<%=cronExp%>"/>分钟
	     </td>
	    <td align="center" >&nbsp;<%=row.get("lastimpdate")%></td>
	    <td align="left">
	    	<label>
	    		<a id="editAction_<%=row.get("jobid")%>" href="javascript:switchEditStatus('<%=row.get("jobid")%>',true);">编辑</a><BR>
	    		<a id="saveAction_<%=row.get("jobid")%>" href="javascript:save('<%=row.get("jobid")%>');" >保存</a><BR>
	    		<a href="javascript:reschuduerTask('<%=row.get("jobid")%>');">保存并启动同步</a><BR>
	    		<a href="javascript:stopSchdulerTask('<%=row.get("jobid")%>');">终止任务</a><BR>
	    		<a href="javascript:deleteSchdulerTask('<%=row.get("jobid")%>');">删除调度任务</a>  
	    	</label>
	    </td>
	  </tr>
  </form>

  <%} %>
</table>
<script language="javascript">
	var currentRecord;
	var xmlHttp;
	function switchEditStatus(jobID,editAble){
		if(editAble) {
			currentRecord=jobID;
			document.getElementById("schuduer_time_" + jobID).disabled=false;
			document.getElementById("editAction_" + jobID).style.display="none";
			document.getElementById("saveAction_" + jobID).style.display="";
		}
		else {
			currentRecord=null;
			document.getElementById("schuduer_time_" + jobID).disabled=true;
			document.getElementById("editAction_" + jobID).style.display="";
			document.getElementById("saveAction_" + jobID).style.display="none";
		}
	}

	function clearStatus() {
		for(var n=0;n<schudler_ctrl.length;n++) {
			document.getElementById(schudler_ctrl[n]).disabled=true;
		}
	}

	function createXMLHttpRequest() {

        //IE不同版本
            if (window.ActiveXObject) {
                try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
                catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}             
            }
        //非IE
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
    }

	function save(jobID){
		var schuduer_time=document.getElementById("schuduer_time_" + jobID).value;
	
		if(schuduer_time==null || isNaN(schuduer_time)) {
			alert("请输入正确的调度时间!");	
			return ;
		}
		
		var url="<%=request.getContextPath()%>/app?ACTION=updateSchduleTask&USERID=<%=userID%>&SIGN=<%=sign%>";
		url=url + "&jobID=" + jobID;
		url=url + "&scheduleTime=" + schuduer_time;
		createXMLHttpRequest();
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open("POST", url, false);
        xmlHttp.send();
	}
	
	function handleStateChange(){
		 if (xmlHttp.readyState == 4) {
             if (xmlHttp.status == 200) {
            	document.location.reload();
             }
         }
	}
	
	function reschuduerTask(jobID){
		var schuduer_time=document.getElementById("schuduer_time_" + jobID).value;
		
		if(schuduer_time==null || isNaN(schuduer_time)) {
			alert("请输入正确的调度时间!");	
			return ;
		}

		var url="<%=request.getContextPath()%>/app?ACTION=reschduleTask&USERID=<%=userID%>&SIGN=<%=sign%>";
		url=url + "&jobID=" + jobID;
		url=url + "&scheduleTime=" + schuduer_time;
		createXMLHttpRequest();
        xmlHttp.onreadystatechange = rescheduleTaskhandleStateChange;
        xmlHttp.open("POST", url, false);
        xmlHttp.send();
	}

	function stopSchdulerTask(jobID){
		var url="<%=request.getContextPath()%>/app?ACTION=stopSchdulerTask&USERID=<%=userID%>&SIGN=<%=sign%>";
		url=url + "&jobID=" + jobID;
		createXMLHttpRequest();
        xmlHttp.onreadystatechange = rescheduleTaskhandleStateChange;
        xmlHttp.open("POST", url, false);
        xmlHttp.send();
	}
	
	function deleteSchdulerTask(jobID){
		var url="<%=request.getContextPath()%>/app?ACTION=deleteSchdulerTask&USERID=<%=userID%>&SIGN=<%=sign%>";
		url=url + "&jobID=" + jobID;
		createXMLHttpRequest();
        xmlHttp.onreadystatechange = rescheduleTaskhandleStateChange;
        xmlHttp.open("POST", url, false);
        xmlHttp.send();
	}
	
	function rescheduleTaskhandleStateChange(){
		if (xmlHttp.readyState == 4) {
			 if (xmlHttp.status == 200) {
	            	document.location.reload();
	         }
        }
	}
</script>
</body>
</html>